package com.qbw.app.module.common.activity;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.LauncherActivity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.qbw.app.Config;
import com.qbw.app.R;
import com.qbw.app.XApplication;
import com.qbw.app.databinding.CommonActivityDeveloperBinding;
import com.qbw.app.manager.UserManager;
import com.qbw.core.base.BaseActivity;
import com.qbw.customview.titlebar.TitleBar;
import com.qbw.spm.P;

import java.util.Calendar;

import cn.jpush.android.service.AlarmReceiver;

/**
 * @author QBW
 * @date 2019/12/17
 */
public class DeveloperActivity extends BaseActivity<CommonActivityDeveloperBinding> implements TitleBar.Listener, View.OnClickListener {

    public static void start(Context context) {
        Intent intent = new Intent(context, DeveloperActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.common_activity_developer;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String env = P.getString(Config.KEY_CURRENT_ENVIRONMENT);
        mBinding.title.setListener(this);
        mBinding.tvCurrent.setText("当前为：" + getEnv(env));
        mBinding.tvDev.setOnClickListener(this);
        mBinding.tvCst.setOnClickListener(this);
        mBinding.tvRelease.setOnClickListener(this);
    }

    private String getEnv(String env) {
        if ("dev".equals(env)) {
            return "开发";
        } else if ("cst".equals(env)) {
            return "测试";
        } else if ("dft".equals(env)) {
            return "生产";
        }
        return "异常";
    }

    @Override
    public void onClick(View v) {
        int vid = v.getId();
        if (vid == R.id.tv_dev) {
            Config.setCurrentEnvironment("dev");
            onChangeEnvironment();
        } else if (vid == R.id.tv_cst) {
            Config.setCurrentEnvironment("cst");
            onChangeEnvironment();
        } else if (vid == R.id.tv_release) {
            Config.setCurrentEnvironment("dft");
            onChangeEnvironment();
        }
    }

    private void onChangeEnvironment() {
        UserManager.getInstance().logout();
        Context context = XApplication.getInstance();
        Intent intent = new Intent(context, LauncherActivity.class);
        intent.setAction(Intent.ACTION_MAIN);
        intent.setPackage(getPackageName());
        PendingIntent sender = PendingIntent.getActivity(context, 0, intent, 0);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 2);
        AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
        System.exit(0);
    }


    @Override
    public void onLeftAreaClick() {
        finish();
    }

    @Override
    public void onRightAreaClick() {

    }

    @Override
    public void onCenterAreaClick() {

    }
}
